Ext.define('yxt.view.ChatWin', {
	  extend: 'Ext.window.Window',
	  alias: 'widget.chatWin',
	  allowRequest: true,//默认可发送请求，若已发请求，则立即置为false
	  allowGetOnline: true,
	  width: window.screen.width*0.5,
	  height: window.screen.height*0.5,
	  name: '',//在IE
	  user_id: '',
	  closable: false,
	  maximizable: true,
	  requires:['yxt.view.FileUploadPhisics'],
	  layout: 'hbox',
	  initComponent: function() {
		  var me = this;
		  window.user_id = me.user_id;
		  window.onbeforeunload = onbeforeunload_handler;   
		  window.onunload = onunload_handler;   
		  me.title = me.name;
		  me.items = [
            me.onlineGrid = Ext.widget('grid',{
            	height: '100%',
            	width: 150,
            	columns: [
            	          {header: '在线列表', dataIndex: 'name', width: 80},
            	          {header: '性别', flex: 1, dataIndex: 'sex', renderer: function(value){
            	        	  if(value == 'M') {
            	        		 return '男'; 
            	        	  }else if(value == 'F') {
            	        		  return '女';
            	        	  }
            	          }}
            	],
            	store: me.onlineStore = Ext.create('yxt.store.OnlineUser')
            }),
            {
            	xtype: 'panel',
            	layout: {
            		type:'vbox',
            		align:'stretch'
            	},
            	tbar: [{
	    			xtype:'textfield',
	    			emptyText: '内容',
	    			enableKeyEvents: true,
	    			itemId:'query',
	    			listeners:{
	    				keypress: function(scope, e) {
							if(e.keyCode == e.ENTER || e.keyCode == 10) {
								me.chatHis.load({
			    					params:{
			    						query: me.down('#query').getValue()
			    					}
			    				});
							}
			        	}
	    			}
	    		},{
	    			text:'搜索',
	    			iconCls: 'yxt-search',
	    			handler:function() {
	    				me.chatHis.load({
	    					params:{
	    						query: me.down('#query').getValue()
	    					}
	    				});
	    			}
	    		},{
	    			text: '编辑',
	    			itemId: 'edit',
	    			hidden: me.user_id!='admin',
	    			iconCls: 'yxt-edit'
	    		},{
	    			text:'删除',
	    			itemId: 'delete',
	    			hidden: me.user_id!='admin',
	    			iconCls: 'yxt-delete'
	    		}],
            	flex: 1,
            	height: '100%',
            	items: [			
            	    me.dataview = Ext.widget('dataview', {
    		    	flex: 1,
    		    	split: true,
    		    	autoScroll:true,
    		    	cls: 'yxt_chat',
    		    	overItemCls: 'x-item-over',
    		    	itemSelector: 'div.yxt_chat_wrap',
    		    	width: '100%',
    		    	itemId: 'chat_his',
    		    	tpl: new Ext.XTemplate(
	    			   '<tpl for=".">',
	    			   	  '<div class="yxt_chat_wrap">',
	    			        '<div class="yxt_float_left">{name} 说：</div>',
	    			        '<div class="yxt_float_right">{[Ext.Date.format(values.chat_date, "Y-m-d H:i:s")]}</div>',
	    			        '<div><font color="#0066CC">{text}</font></div>',
	    			       '</div>', 
	    			    '</tpl>'
	    			),
    		    	store: me.chatHis = Ext.create('yxt.store.ChatHis')
    		    }),
    		    me.textPanel = Ext.widget('panel', {
    				layout: 'hbox',
    				tbar: [{
    	       			xtype: 'fileUploadP',
    	       			itemId: 'pic',
    					onlyImg: true,
    					border: 0,
    					extraParams: {filePath:'pics\\', type: 'pic'},
    					fileFieldConfig: {
    						buttonOnly: true,
    						buttonConfig: {
    							text: '发送图片',
    							iconCls: 'yxt-insert-image'								
    						}
    					}
    	   		 },{
 	       			xtype: 'fileUploadP',
 	       			itemId: 'file',
 					border: 0,
 					extraParams: {filePath:'files\\'},
 					fileFieldConfig: {
 						buttonOnly: true,
 						buttonConfig: {
 							text: '发送文件',
 							iconCls: 'yxt-insert-file'								
 						}
 					}
    	   		 }],
    	   		 height: 100,
    				split: true,
    				width: '100%',	
    				items: [
    				        {
    				        	xtype: 'textarea', 
    				        	name: 'text', 
    				        	flex: 1, 
    				        	height: '100%',
    				        	labelAlign: 'right',
    				        	enableKeyEvents: true
    				        },
    				        {
    				        	xtype: 'button',
    				        	text: '<font size=3>发言</font>',
    				        	itemId:'send',
    				        	width: 100,
    				        	height: '100%'
    				        }]
    			})]
            }];
		  me.textPanel.down('textfield').focus();
		  me.callParent(arguments);
	  },
	/**
	 * 
	 * @param params 请求的参数
	 */
	sendRequest: function(params) {
		var me = this;		
		if(params) {
			Ext.apply(params, {		
				user_id: me.user_id	
			});
		} else {
			params = {
				user_id: me.user_id
			};
		}

		if(me.allowRequest) {
			Ext.Ajax.request({
				url: 'message/getNewMsgList.do',
				method: 'POST',
				params: params,
				timeout: 120000000,
				success: function(response) {
					var obj = Ext.decode(response.responseText);
					me.chatHis.insert(0, obj.data);
					me.allowRequest = true;
					me.sendRequest(null);
				},
				failure: function() {
					me.sendRequest(null);
				}
			});
			me.allowRequest = false;
		}
		
	  },
	  getOnline: function(logined) {
		  var me = this;
		  Ext.Ajax.request({
				url: 'user/getOnlineList.do',
				method: 'POST',
				params: {
					logined: logined
				},
				timeout: 120000000,
				success: function(response) {
					var obj = Ext.decode(response.responseText);
					me.onlineStore.loadData(obj.onlineList);
					me.getOnline(true);
				},
				failure: function(response) {
					me.getOnline(true);
				}
		});
	  } 
});
function onbeforeunload_handler(){
    Ext.Ajax.request({
    	url: 'user/logout.do',
    	params: {
    		user_id: window.user_id
    	}
    });  
}   
   
function onunload_handler(){   
	
}   